﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using Communication;
using Core;
using Core.DataProvider;

namespace Service.Forwarder
{
    class Monitoring : IMonitoring
    {
        public bool AddAlert(AlertRule alertRule, Entity entity, EntityProperty entityProperty)
        {
            bool result = false;
            Logging.LogInformation("Alert Received.");
            ForwarderService.sqlConnector.AddAlert(alertRule, entity, entityProperty);
            result = true;
            return result;
        }

        public int AddEntity(Entity entity)
        {            
            Logging.LogInformation("Entity Received...");
            // This code is added because AddEntity does not work if SqlConnector is globally instaniated
            // begin
            //int r = -1;
            //using (SqlConnector sc = new SqlConnector())
            //{
            //    r = sc.AddEntity(entity);
            //}
            //return r;
            // einde
            return ForwarderService.sqlConnector.AddEntity(entity); 
        }

        public int AddEntityProperty(EntityProperty entityProperty)
        {
            Logging.LogInformation("EntityProperty Received...");
            //bool result = false;
            //using (SqlConnector sc = new SqlConnector())
            //{
            //    sc.AddEntityProperty(entityProperty);
            //    result = true;
            //}
            //return result;
            return ForwarderService.sqlConnector.AddEntityProperty(entityProperty);
        }

        public EntityType GetEntityTypeByUid(string uid)
        {
            return new EntityType();
        }

        public List<EntityType> GetEntityTypeList()
        {
            //used to test the Reader builder cache. the First time the builder is created, the second and thirt come from cache.
            //SqlConnector.Reader<EntityType>("dbo.GetEntityTypes", null);
            //SqlConnector.Reader<EntityType>("dbo.GetEntityTypes", null);

            return SqlConnector.Reader<EntityType>("dbo.GetEntityTypes", null);
        }

        public List<EntityTypeProperty> GetEntityTypePropertyList()
        {
            return SqlConnector.Reader<EntityTypeProperty>("dbo.GetEntityTypeProperties", null);
        }

        public List<dimPerfMon> GetPerfMonList()
        {
            return SqlConnector.Reader<dimPerfMon>("dbo.GetEntityTypeProperties", null);
        }

        public List<dimDate> GetDateList()
        {
            return SqlConnector.Reader<dimDate>("dbo.GetEntityTypeProperties", null);
        }

        public List<AlertRule> GetAlertRuleList()
        {
            return SqlConnector.Reader<AlertRule>("dbo.GetAlertRules", null);
        }

        public List<dimPerfMonCounter> GetPerfMonCounterList()
        {
            return SqlConnector.Reader<dimPerfMonCounter>("dbo.GetEntityTypeProperties", null);
        }

        public List<DeviationPercent> GetDeviationPercentList()
        {
            return SqlConnector.Reader<DeviationPercent>("dbo.GetDeviationPercent", null);
        }
    }
}
